package com.example.shiwuyouproject.ui.fragment.my.activity

import androidx.lifecycle.Observer
import androidx.lifecycle.observe
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityTeachingtimeBinding
import com.example.shiwuyouproject.ui.fragment.my.activity.adapter.LefitTimeAdapter
import com.example.shiwuyouproject.ui.fragment.my.activity.adapter.RightWeekAdapter
import com.example.shiwuyouproject.ui.fragment.my.activity.bean.*
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.TeachingTimeViewModel
import com.example.shiwuyouproject.ui.pup.AlbumPopupView
import com.example.shiwuyouproject.ui.pup.TimePickPopup
import com.example.shiwuyouproject.utils.GlideEngine
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.lxj.xpopup.XPopup
import kotlinx.android.synthetic.main.activity_subject.*
import java.util.*
import kotlin.collections.ArrayList

/**
 * 授课时间
 */
class TeachingTimeActivity : BaseVmActivity<ActivityTeachingtimeBinding, TeachingTimeViewModel>() {
    override fun viewModelClass(): Class<TeachingTimeViewModel> = TeachingTimeViewModel::class.java

    override fun getViewBinding(): ActivityTeachingtimeBinding {
        return ActivityTeachingtimeBinding.inflate(layoutInflater)
    }

    override fun initView() {
        mBinding.myTitleBar.tvTitle.text = "设置授课时间"
    }

    private var timeDateAll: MutableList<LefitTimeBean> = ArrayList()
    private var timeDate: MutableList<LefitTimeBean> = ArrayList()
    private var myLeftTimeAdapter: LefitTimeAdapter? = null
    private var rightWeekAdapter: RightWeekAdapter? = null
    val myDataAll: MutableList<RightWeekBean> = ArrayList()
    override fun initData() {
        timeDateAll.add(LefitTimeBean("08:00-10:00", 8, 10, false))
        timeDateAll.add(LefitTimeBean("10:00-12:00", 10, 12, false))
        timeDateAll.add(LefitTimeBean("12:00-14:00", 12, 14, false))
        timeDateAll.add(LefitTimeBean("14:00-16:00", 14, 16, false))
        timeDateAll.add(LefitTimeBean("16:00-18:00", 16, 18, false))
        timeDateAll.add(LefitTimeBean("18:00-20:00", 18, 20, false))
        timeDateAll.add(LefitTimeBean("20:00-22:00", 20, 22, false))
        myLeftTimeAdapter = LefitTimeAdapter()
        mBinding.myLeftTime.adapter = myLeftTimeAdapter
        myLeftTimeAdapter?.setNewData(timeDate)
        rightWeekAdapter = RightWeekAdapter()
        mBinding.myRightWeek.adapter = rightWeekAdapter
        rightWeekAdapter?.setNewData(myDataAll)
        mViewModel.courseLists()
    }

    override fun setListener() {
        mBinding.myTitleBar.tvBack.setOnClickListener { finish() }
        mBinding.clCheckBox.setOnClickListener {
            mBinding.ivCheckBox.isSelected = !mBinding.ivCheckBox.isSelected
            if (mBinding.ivCheckBox.isSelected) {
                timeDate.clear()
                timeDateAll.forEachIndexed { index, lefitTimeBean ->
                    val item = LefitTimeBean("自定义")
                    timeDate.add(item)
                }
                myLeftTimeAdapter?.setNewData(timeDate)
                rightWeekAdapter?.data?.forEach {
                    it.menuData.forEachIndexed { index, rightMenuBean ->
                        rightMenuBean.myStatus = false
                    }
                }
                rightWeekAdapter?.notifyDataSetChanged()
            } else {
                timeDate.clear()
                timeDateAll.forEach {
                    timeDate.add(it)
                }
                myLeftTimeAdapter?.setNewData(timeDate)
                rightWeekAdapter?.data?.forEach {
                    it.menuData.forEachIndexed { index, rightMenuBean ->
                        rightMenuBean.myStatus = false
                    }
                }
                rightWeekAdapter?.notifyDataSetChanged()
            }
        }
        myLeftTimeAdapter?.setOnItemClickListener { adapter, view, position ->
            if (!mBinding.ivCheckBox.isSelected) {//非自定义状态拦截
                return@setOnItemClickListener
            }
            XPopup.Builder(this)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .asCustom(TimePickPopup(this, object : TimePickPopup.onTimeOnClickListener {
                    override fun onTimeBack() {
                    }
                }))
                .show()
        }
    }

    override fun observe() {
        mViewModel.courseListsState.observe(this,
            Observer { it ->
                mBinding.ivCheckBox.isSelected = it.data.custom != 0
                if (it.data.custom == 1) {//自定义时间处理
                    it.data.list.forEach {
                        addTimeBean(it)
                    }
                    if (timeDate.size != 6) {//处理时间日期不够显示自定义问题
                        var index = 6
                        index -= timeDate.size
                        for (i in 0..index) {
                            timeDate.add(LefitTimeBean("自定义"))
                        }
                    }
                    myLeftTimeAdapter?.notifyDataSetChanged()
                    timeDate.forEach { _ ->
                        val myWeekTime: MutableList<RightMenuBean> = ArrayList()
                        for (i in 0..6) {
                            myWeekTime.add(RightMenuBean(false, i + 1))
                        }
                        myDataAll.add(RightWeekBean(myWeekTime))
                    }
                    timeDate.forEachIndexed { index, lefitTimeBean ->
                        lefitTimeBean.weekData.forEachIndexed { weekIndex, i ->
                            myDataAll[index].menuData[(i - 1)].myStatus = true
                            myDataAll[index].menuData[(i - 1)].week = i
                        }
                    }
                    rightWeekAdapter?.notifyDataSetChanged()
                }else{
                    it.data.list.forEach {
                        val stTime = it.start_time + "-" + it.end_time
                        if(backStTimeStatus(stTime)){
                            timeDate[backStTimeStatusIndex].weekData.add(it.week_day)
                        }else{
                            timeDate.add(LefitTimeBean(stTime))
                            timeDate[timeDate.size-1].weekData.add(it.week_day)
                        }
                    }
                    timeDateAll.forEachIndexed { index, lefitTimeBean ->
                        if(index>=timeDate.size){
                            timeDate.add(lefitTimeBean)
                        }
                    }
                    myLeftTimeAdapter?.notifyDataSetChanged()
                    timeDate.forEach { _ ->
                        val myWeekTime: MutableList<RightMenuBean> = ArrayList()
                        for (i in 0..6) {
                            myWeekTime.add(RightMenuBean(false, i + 1))
                        }
                        myDataAll.add(RightWeekBean(myWeekTime))
                    }
                    timeDate.forEachIndexed { index, lefitTimeBean ->
                        lefitTimeBean.weekData.forEachIndexed { weekIndex, i ->
                            myDataAll[index].menuData[(i - 1)].myStatus = true
                            myDataAll[index].menuData[(i - 1)].week = i
                        }
                    }
                    rightWeekAdapter?.notifyDataSetChanged()
                }
            })
    }

    /**
     * 自定义状态下处理
     * 通过判断同样时间不同星期处理
     */
    private fun addTimeBean(courseBean: MyData) {
        val stTime = courseBean.start_time + "-" + courseBean.end_time
        if (timeDate.size == 0) {
            timeDate.add(LefitTimeBean(stTime))
            timeDate[0].weekData.add(courseBean.week_day)
        } else {
            if(backStTimeStatus(stTime)){
                timeDate[backStTimeStatusIndex].weekData.add(courseBean.week_day)
            }else{
                timeDate.add(LefitTimeBean(stTime))
                timeDate[timeDate.size-1].weekData.add(courseBean.week_day)
            }
        }
    }
    private var backStTimeStatusIndex=0
    /**
     * 反馈当前time_Data数组中是否包含该数据
     */
    private fun backStTimeStatus(stTime: String): Boolean {
        timeDate.forEachIndexed { index, lefitTimeBean ->
            if (stTime == lefitTimeBean.stTime) {
                backStTimeStatusIndex=index
                return true
            }
        }
        return false
    }
}